﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DCS.Business.Model;
using DCS.DataProvider;
using DCS.Business.Model.PublishUsage;
using CarolLib;
using System.Configuration;

namespace DCS.Business.Manager
{
	public class PublishManager
	{
		private IDataProvider provider;
		static log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

		public PublishManager(IDataProvider p)
		{
			this.provider = p;
		}

		private static readonly string KEY_GetFlightPassengerInfo = "PI-D-{0}{1}-{2}-{3}{4}";

		private static string GetCacheKey(string carrier, string flightNo, DateTime pdate, string dept, string arr)
		{
			return string.Format(KEY_GetFlightPassengerInfo, carrier, flightNo, pdate.ToShortDateString(), dept, arr);
		}
		protected int CacheSeconds
		{
			get
			{
				if (ConfigurationManager.AppSettings["CacheSeconds"] != string.Empty)
				{
					return int.Parse(ConfigurationManager.AppSettings["CacheSeconds"]);
				}
				else
				{
					return 1;
				}
			}
		}

		public FlightPassengerInfo GetFlightPassengerInfo(string carrier, string flightNo, DateTime pdate, string dept, string arr)
		{
			FlightPassengerInfo result = null;
            string sql = string.Empty;

			if (!CacheHelper.TryGet<FlightPassengerInfo>(GetCacheKey(carrier, flightNo, pdate, dept, arr), out result))
			{
				try
				{
                    //modified by RenWenJie 2011-07-20
                    if (pdate <= DateTime.Today.AddDays(-7))
                    {
                        sql = string.Format(ConfigurationManager.AppSettings["SQL_GetFlightPassengerInfoHis"], carrier, flightNo, pdate.ToShortDateString(), dept, arr);
                    }
                    else
                    {
                        sql = string.Format(ConfigurationManager.AppSettings["SQL_GetFlightPassengerInfo"], carrier, flightNo, pdate.ToShortDateString(), dept, arr);
                    }
					result = provider.GetFlightPassengerInfo(sql);

					CacheHelper.Insert(GetCacheKey(carrier, flightNo, pdate, dept, arr), result, CacheSeconds);
				}
				catch (Exception e)
				{
					Logger.Error("Error During GetFlightPassengerInfo", e);
				}
			}
			return result;
		}

        public List<DCSFileGroup> GetSyncMessagesByMessageType(out MessageGroup messageList, string messageType , int groupCount = 100)
        {
            messageList = new MessageGroup();

            List<DCSFileGroup> result;


            switch(messageType)
            {
                case "PRL":
                    List<PRLMessage> prlList;
                    result= GetSyncMessagesByMessageType(out prlList, messageType, groupCount);
                    messageList.PRLList=prlList;
                    break;

                case "PFS":
                    List<PFSMessage> pfsList;
                    result = GetSyncMessagesByMessageType(out pfsList, messageType, groupCount);
                    messageList.PFSList=pfsList;
                    break;
                default:
                    result = null;
                    break;
            }

            return result;

        }

		public List<DCSFileGroup> GetSyncMessagesByMessageType(out List<PRLMessage> prlList, string messageType = "PRL", int groupCount = 100)
		{
			List<DCSFileGroup> result = new List<DCSFileGroup>();

			List<DCSFile> list = provider.GetSyncMessagesByMessageType(out prlList, messageType, groupCount);

			var groups = list.GroupBy(d => d.RemoteFileName.Substring(0, d.RemoteFileName.IndexOf("_PART")));

			foreach (var item in groups)
			{
				var g = new DCSFileGroup();
				g.AddRange(item.ToList());
				result.Add(g);
			}

			return result;
		}

		public List<DCSFileGroup> GetSyncMessagesByMessageType(out List<PFSMessage> pfsList, string messageType = "PFS", int groupCount = 100)
		{
			List<DCSFileGroup> result = new List<DCSFileGroup>();

			List<DCSFile> list = provider.GetSyncPFSMessagesByMessageType(out pfsList, messageType, groupCount);

			var groups = list.GroupBy(d => d.RemoteFileName.Substring(0, d.RemoteFileName.IndexOf("_PART")));

			foreach (var item in groups)
			{
				var g = new DCSFileGroup();
				g.AddRange(item.ToList());
				result.Add(g);
			}

			return result;
		}

        public void InsertLogList(List<ESBSyncLog> logList)
        {
            provider.InsertSyncLog(logList);
        }
	}
}
